package com.meida.module.bus.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.constants.CommonConstants;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.module.bus.client.entity.BusSubject;
import com.meida.module.bus.provider.mapper.BusSubjectMapper;
import com.meida.module.bus.provider.service.BusSubjectService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 专业接口实现类
 *
 * @author flyme
 * @date 2021-08-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusSubjectServiceImpl extends BaseServiceImpl<BusSubjectMapper, BusSubject> implements BusSubjectService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusSubject subject, EntityMap extra) {
        String subjectTitle = subject.getSubjectTitle();
        Long num = countNumByTitle(subjectTitle);
        if (num > 0) {
            return ResultBody.failed("该标签已存在");
        }
        subject.setState(CommonConstants.DISABLED);
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusSubject> cq, BusSubject subject, EntityMap requestMap) {
        cq.orderByDesc("subject.createTime");
        return ResultBody.ok();
    }

    @Override
    public Long countNumByTitle(String subjectTitle) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("subjectTitle", subjectTitle);
        return count(qw);
    }

    @Override
    public BigDecimal getPriceById(Long subjectId) {
        CriteriaQuery cq = new CriteriaQuery(BusSubject.class);
        cq.select("amount");
        cq.eq("subjectId", subjectId);
        EntityMap one = findOne(cq);
        return one.getBigDecimal("amount");
    }

    @Override
    public BigDecimal getPriceByIds(List<Long> subjectIds) {
        CriteriaQuery cq = new CriteriaQuery(BusSubject.class);
        cq.select("amount");
        cq.in("subjectId", subjectIds);
        cq.orderByDesc("amount");
        cq.last("limit 1");
        EntityMap one = findOne(cq);
        return one.getBigDecimal("amount");
    }

    @Override
    public EntityMap getSimpleSubjectInfo(Long subjectId) {
        CriteriaQuery cq = new CriteriaQuery(BusSubject.class);
        cq.select("subjectId", "subjectTitle", "amount");
        cq.eq(true, "subjectId", subjectId);
        EntityMap one = findOne(cq);
        return one;
    }

    @Override
    public List<String> selectSubjectTitles(String subjectIds) {
        if (FlymeUtils.isEmpty(subjectIds)) {
            return Lists.newArrayList();
        }
        CriteriaQuery cq = new CriteriaQuery(BusSubject.class);
        cq.select("subjectTitle");
        cq.in("subjectId", subjectIds.split(","));
        return listObjs(cq, i -> i.toString());
    }

    @Override
    public ResultBody checkState(Long subjectId, Integer state) {
        BusSubject busSubject = getById(subjectId);
        busSubject.setState(state);
        boolean result = updateById(busSubject);
        if (result) {
            return ResultBody.ok("操作成功");
        }
        return ResultBody.failed("操作失败");
    }


}
